package com.open.capacity.activity.service.impl;

import com.open.capacity.activity.dao.AUserRoleDao;
import com.open.capacity.activity.dao.ActivityDao;
import com.open.capacity.activity.service.ActivityService;
import com.open.capacity.common.model.Activity;
import com.open.capacity.common.model.SysRole;
import com.open.capacity.common.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: open-capacity-platform
 * @description:
 * @author: bellweather
 * @create: 2021-01-18 16:22
 **/

@Service
public class ActivityServiceimpl implements ActivityService {
    @Autowired
    private ActivityDao activityDao;

    @Autowired
    private AUserRoleDao aUserRoleDao;
    @Override
    public List<Activity> getList(Map<String, Object> params) {
        Object userId1 = params.get("userId");
        long userId = 0;
        boolean isAdmin=false; //默认不是管理员
        if(userId1!=null){
            userId = Long.valueOf(params.get("userId").toString());
            Set<SysRole> rolesByUserId = aUserRoleDao.findRolesByUserId(userId);
            Iterator<SysRole> iterator = rolesByUserId.iterator();
            while (iterator.hasNext()){
                SysRole next = iterator.next();
                if(next.getCode().equals("ADMIN")){
                    isAdmin=true;
                    break;
                }
            }
        }

        if(isAdmin) {
            params.remove("userId");
        }else {
            SysUser user = aUserRoleDao.findUserById(userId);
            Long companyId = user.getCompanyId();
            params.remove("userId");
            params.put("companyId",companyId);
        }

        return activityDao.getList(params);
    }

    @Override
    public int save(Activity activity) {
        return activityDao.save(activity);
    }

    @Override
    public int update(Activity activity) {
        return activityDao.update(activity);
    }

    @Override
    public int delete(int id,int deleted) {
        return activityDao.delete(id,deleted);
    }

    @Override
    public int realdelete(int id) {
        return activityDao.realDel(id);
    }
}
